package com.example.cr.insect.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.cr.common.response.PageResponse;
import com.example.cr.insect.entity.PestIdentification;
import com.example.cr.insect.entity.PestIdentificationExample;
import com.example.cr.insect.mapper.PestIdentificationMapper;
import com.example.cr.insect.request.PestIdentificationListRequest;
import com.example.cr.insect.response.PestIdentificationResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
    import cn.hutool.core.date.DateTime;
    import cn.hutool.core.util.ObjectUtil;
    import com.example.cr.common.util.SnowflakeUtil;
    import com.example.cr.insect.request.PestIdentificationRequest;

@Service
public class PestIdentificationService {
private static final Logger log = LoggerFactory.getLogger(PestIdentificationService.class);
@Autowired
PestIdentificationMapper pestIdentificationMapper;

public PageResponse<PestIdentificationResponse> list(PestIdentificationListRequest request) {
    PestIdentificationExample pestIdentificationExample = new PestIdentificationExample();
    pestIdentificationExample.setOrderByClause("id desc");

    // 处理搜索条件
    PestIdentificationExample.Criteria criteria = pestIdentificationExample.createCriteria();
    if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
    String keyword = "%" + request.getKeyword() + "%";
    }

    PageHelper.startPage(request.getPage(), request.getSize());
    log.info("当前页码 current_page = {}", request.getPage());
    log.info("每页记录数 per_page = {}", request.getSize());

    List<PestIdentification> pestIdentifications = pestIdentificationMapper.selectByExample(pestIdentificationExample);

    PageInfo<PestIdentification> pageInfo = new PageInfo<>(pestIdentifications);
    log.info("总记录数 total = {}", pageInfo.getTotal());
    log.info("总页数 pages = {}", pageInfo.getPages());

    List<PestIdentificationResponse> list = BeanUtil.copyToList(pestIdentifications, PestIdentificationResponse.class);

        PageResponse<PestIdentificationResponse> pageResponse = new PageResponse<>();
            pageResponse.setTotal(pageInfo.getTotal());
            pageResponse.setList(list);

            return pageResponse;
            }
            public void save(PestIdentificationRequest request) {
            PestIdentification pestIdentification = BeanUtil.copyProperties(request, PestIdentification.class);
            DateTime now = DateTime.now();
            if (ObjectUtil.isNull(pestIdentification.getId()) || pestIdentification.getId() == 0L) {
            pestIdentification.setId(SnowflakeUtil.getId());
            pestIdentification.setCreatedAt(now);
            pestIdentification.setUpdatedAt(now);
            pestIdentificationMapper.insert(pestIdentification);
            } else {
            pestIdentification.setUpdatedAt(now);
            pestIdentificationMapper.updateByPrimaryKeySelective(pestIdentification);
            }
            }

            public int deleteBatch(List<Long> ids) {
                PestIdentificationExample pestIdentificationExample = new PestIdentificationExample();
                PestIdentificationExample.Criteria criteria = pestIdentificationExample.createCriteria();
                criteria.andIdIn(ids);
                return pestIdentificationMapper.deleteByExample(pestIdentificationExample);
                }
                }
